<?php

  /* this script performs tasks that are part of the transportation application.
     All tasks in here are mainly processing and credential-checking processes.
	 A process should be written as a module in the module file if its main 
	 purpose is displaying information to user. */

 switch ($_REQUEST['task']) {
   case 'changemenu':
//     if($_SESSION[$_REQUEST['menu']] > 0) {
	   $_SESSION['load_menu'] = $_REQUEST['menu'];
//     } else {
//	   echo "Sorry, but you are not allowed to view the `" . $_REQUEST['menu'] . "' menu.";
//	 }
	 break;
   case 'workswap_post':
       $sql = sprintf("INSERT INTO `workswap` (`shift_id`,`shift_date`,`old_driver`,`temp_perm`,`date_offered`,`comments`,`redstar`)
	   					VALUES ( %s, %s, %s, %s, %s, %s, %s)",
	   					quote_smart($_REQUEST['shift']),
						quote_smart($_REQUEST['year'] . $_REQUEST['month'] . $_REQUEST['day']),
						quote_smart($_SESSION['driver_id']),
						quote_smart($_REQUEST['TempOrPerm']),
						quote_smart(date("Ymd")),
						quote_smart($_REQUEST['comment']),
						quote_smart($_REQUEST['redStar']));
	   $result = mysql_query($sql);
	   if(mysql_affected_rows()) {
	     $javascript =  '<script language=javascript>msgBox("Your shift has been added to the workswap board.")</script>';
	   } else {
	     $javascript =  '<script language=javascript>msgBox("An error occurred.")</script>';
		 print "<!-- $sql -->\n";
	   }
	 break;
   case 'workswap_take':
   		//grab current data about shift
		$result = mysql_query(sprintf("SELECT * FROM `workswap` WHERE `workswap_id` = %s", quote_smart($_REQUEST['wsid'])));
		if(mysql_num_rows($result) == 1) {
			$data = mysql_fetch_assoc($result);
			//check if it is partial or not
			if($data['shift_start']) {
				//it is partial
			} else {
				//check if it is temp or perm
				if($data['temp_perm'] == 0) {
					//it's temp
					$update_sql = sprintf("UPDATE `workswap` SET `driver_id_new` = %s, `date_taken` = %s WHERE `workswap_id` = %s LIMIT 1",
									quote_smart($_SESSION['driver_id']),
									quote_smart(date("Ymd")),
									quote_smart($_REQUEST['wsid']));
									print "<!-- $update_sql -->\n";
				} else if ($data['temp_perm'] == 1) {
					//it's perm
					$update_sql = sprintf("UPDATE `workswap` SET `driver_id_new` = %s, `date_taken` = %s WHERE `workswap_id` = %s LIMIT 1",
									quote_smart($_SESSION['driver_id']),
									quote_smart(date("Ymd")),
									quote_smart($_REQUEST['wsid']));
					//update the schedule shifts
					$update_sched = sprintf("UPDATE `t_sched_shifts` SET `driver_id` = %s WHERE `shift_id` = %s",
									quote_smart($_SESSION['id']),
									quote_smart($data['shift_id']));
					$sched_result = mysql_query($update_sched);
				}
				$result = mysql_query($update_sql);
				//figure out if the changes went through
				if(mysql_affected_rows()) {
					$javascript =  '<script language=javascript>msgBox("You have taken the shift.")</script>';
				} else {
					$javascript =  '<script language=javascript>msgBox("An error occurred.")</script>';
					print "<!-- $sql -->\n";
				}
			} //end partial if
		}
	 break;
   case 'acceptRules':
	$group = $_REQUEST['group_id'];
	$driver = $_SESSION['driver_id'];
	$sql = sprintf("UPDATE `t_sched_signups` SET `accept_rules` = 1 WHERE `driver_id` = %s AND `group_id` = %s LIMIT 1",quote_smart($driver),quote_smart($group));
	$result = mysql_query($sql);
	 break;
   case 'deleteShift':
	$sql = sprintf("DELETE FROM `t_sched_shifts` WHERE `shift_id` = %s LIMIT 1",
				quote_smart($_REQUEST['shift_id']));
	print "<!-- $sql -->\n";
	$result = mysql_query($sql);
	if(mysql_affected_rows() != 1) {
		$status = '<span style="display:block; margin:20px; background-color:#FFFFCC; border: 1px solid #FFFF00; padding: 10px; text-align:center; font-weight: bold;">Couldn\'t delete shift ' . $_REQUEST['shift_id'] . '</span>';
	} else {
		$status = '<span style="display:block; margin:20px; background-color:#FFFFCC; border: 1px solid #FFFF00; padding: 10px; text-align:center; font-weight: bold;">Successfully deleted shift ' . $_REQUEST['shift_id'] . '</span>';
	}
	break;
 }
 
 ?>
 
